package com.sxm.yiwei.fragments;


import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;

import android.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.sxm.yiwei.R;

/** 订单页面
 * A simple {@link Fragment} subclass.
 */
public class ShopFragment extends BaseFragment implements View.OnClickListener{

    private Button vendor,buyer,order,bill;
    private ShopBillFragment billFragment;
    private ShopBuyerFragment buyerFragment;
    private ShopOrderFragment orderFragment;
    private ShopVendorFragment vendorFragment;
    private PasssengerFragment passsengerFragment;
    private CarOwnerFragment carOwnerFragment;
    private Button passenger;
    private Button carowner;
    private TextView pickbt;
    private TextView carbt;
    private TextView servicebt;

    public ShopFragment() {
        // Required empty public constructor
    }

    private void initShopView(View view) {
        vendor = (Button) view.findViewById(R.id.shop_vendor);
        buyer = (Button) view.findViewById(R.id.shop_buyer);
        order = (Button) view.findViewById(R.id.shop_order);
        bill = (Button) view.findViewById(R.id.shop_bill);
        passenger = (Button) view.findViewById(R.id.shop_passsenger);
        carowner = (Button) view.findViewById(R.id.shop_carowner);
        pickbt = (TextView) view.findViewById(R.id.shop_title_pickbt);
        carbt = (TextView) view.findViewById(R.id.shop_title_carbt);
        servicebt = (TextView) view.findViewById(R.id.shop_title_servicebt);

        //设置点击事件
        vendor.setOnClickListener(this);
        buyer.setOnClickListener(this);
        order.setOnClickListener(this);
        bill.setOnClickListener(this);
        carowner.setOnClickListener(this);
        passenger.setOnClickListener(this);
        pickbt.setOnClickListener(this);
        carbt.setOnClickListener(this);
        servicebt.setOnClickListener(this);


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_shop, container, false);
        // 初始化控件
        initShopView(view);
        selectFragment(1);

        servicebt.setSelected(true);
        vendor.setVisibility(View.VISIBLE);
        buyer.setVisibility(View.VISIBLE);
        vendor.setSelected(true);
        return view;
    }

    private void selectFragment(int i) {
        FragmentManager fragmentManager = getFragmentManager();
       FragmentTransaction fft = fragmentManager.beginTransaction();
        hideFragment(fft);
        switch (i){
            case 1:
                if (vendorFragment==null){
                    vendorFragment = new ShopVendorFragment();
                    fft.add(R.id.shop_content,vendorFragment);
                }else {
                    fft.show(vendorFragment);
                }
                break;
            case 2:
                if (buyerFragment == null) {
                    buyerFragment = new ShopBuyerFragment();
                    fft.add(R.id.shop_content,buyerFragment);
                }else {
                    fft.show(buyerFragment);
                }
                break;
            case 3:
                if (orderFragment==null){
                    orderFragment = new ShopOrderFragment();
                    fft.add(R.id.shop_content,orderFragment);
                }else {
                    fft.show(orderFragment);
                }
                break;
            case 4:
                if (billFragment==null){
                    billFragment = new ShopBillFragment();
                    fft.add(R.id.shop_content,billFragment);
                }else {
                    fft.show(billFragment);
                }
                break;
            case 5:
                if (passsengerFragment==null){
                    passsengerFragment = new PasssengerFragment();
                    fft.add(R.id.shop_content,passsengerFragment);
                }else {
                    fft.show(passsengerFragment);
                }
                break;
            case 6:
                if (carOwnerFragment==null){
                    carOwnerFragment = new CarOwnerFragment();
                    fft.add(R.id.shop_content,carOwnerFragment);
                }else {
                    fft.show(carOwnerFragment);
                }
                break;
        }

        fft.commit();


    }

    private void hideFragment(FragmentTransaction fft) {
        if (orderFragment!=null){
            fft.hide(orderFragment);
        }
        if (billFragment!=null){
            fft.hide(billFragment);
        }
        if (buyerFragment!=null){
            fft.hide(buyerFragment);
        }
        if (vendorFragment!=null){
            fft.hide(vendorFragment);
        }
        if (passsengerFragment!=null){
            fft.hide(passsengerFragment);
        }
        if (carOwnerFragment!=null){
            fft.hide(carOwnerFragment);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.shop_title_servicebt:
                servicebt.setSelected(true);
                pickbt.setSelected(false);
                carbt.setSelected(false);
                vendor.setVisibility(View.VISIBLE);

                buyer.setVisibility(View.VISIBLE);
                vendor.setSelected(true);
                buyer.setSelected(false);
                bill.setVisibility(View.GONE);
                order.setVisibility(View.GONE);
                passenger.setVisibility(View.GONE);
                carowner.setVisibility(View.GONE);
                selectFragment(1);
                break;
            case R.id.shop_title_carbt:
                servicebt.setSelected(false);
                pickbt.setSelected(false);
                carbt.setSelected(true);
                vendor.setVisibility(View.GONE);
                buyer.setVisibility(View.GONE);
                bill.setVisibility(View.GONE);
                order.setVisibility(View.GONE);
                passenger.setVisibility(View.VISIBLE);
                carowner.setVisibility(View.VISIBLE);
                passenger.setSelected(true);
                carowner.setSelected(false);
                selectFragment(5);

                break;
            case R.id.shop_title_pickbt:
                servicebt.setSelected(false);
                pickbt.setSelected(true);
                carbt.setSelected(false);
                vendor.setVisibility(View.GONE);
                buyer.setVisibility(View.GONE);
                bill.setVisibility(View.VISIBLE);
                order.setVisibility(View.VISIBLE);
                passenger.setVisibility(View.GONE);
                carowner.setVisibility(View.GONE);
                order.setSelected(true);
                bill.setSelected(false);
                selectFragment(3);
                break;
            case R.id.shop_vendor:
                vendor.setSelected(true);
                buyer.setSelected(false);
                order.setSelected(false);
                bill.setSelected(false);
                selectFragment(1);
                break;
            case R.id.shop_buyer:
                selectFragment(2);
                vendor.setSelected(false);
                buyer.setSelected(true);
                order.setSelected(false);
                bill.setSelected(false);
                break;
            case R.id.shop_order:
                selectFragment(3);
                vendor.setSelected(false);
                buyer.setSelected(false);
                order.setSelected(true);
                bill.setSelected(false);
                break;
            case R.id.shop_bill:
                selectFragment(4);
                vendor.setSelected(false);
                buyer.setSelected(false);
                order.setSelected(false);
                bill.setSelected(true);
                break;
            case R.id.shop_passsenger:
                selectFragment(5);
                passenger.setSelected(true);
                carowner.setSelected(false);
                break;
            case R.id.shop_carowner:
                selectFragment(6);
                passenger.setSelected(false);
                carowner.setSelected(true);

                break;
        }
    }
}
